/**
 * Created by yuxinhai on 2016/12/21.
 */

'use strict'

let mor = require("../libs/mongo/mongoHelper");
let querystring = require('querystring');
let dbr = require("../libs/mysql/mysqlHelper");
let jv = require("../libs/sys/myHelper").jv;
let http = require('request-promise');

app.use(app.routes.get('/html/ayi/order_detail', async ctx => {
  ctx.render("ayi/order_detail");
}));

app.use(app.routes.get('/html/ayi/message_list', async ctx => {
  ctx.render("message/list");
}));
app.use(app.routes.get('/html/ayi/message_detail', async ctx => {
  ctx.render("message/detail");
}));

app.use(app.routes.post('/ayi/order_detail', async ctx => {

  let userId = ctx.needLogin();
  if (!userId) {
    return;
  }

  let orderId = ctx.req.json.orderId;

  if (!orderId) {
    ctx.msg = "找不到订单Id";
    return;
  }

  let results = await dbr.findOne(`select o.id, f.name fuwuName , o.status , o.amount_payable total ,o.contact_name contactor,o.mobile , o.detailed_address address , o.is_upload_video , fw.username ayiName ,usr.name wuyeName
from thwy_order o
join thwy_fuwu f on (o.fuwu_id = f.id)
join  thwy_order_user ou  on ( o.id = ou.order_id and (ou.\`status\` = 'waiting' or ou.\`status\` = 'completed') )
join sys_user usr on ( ou.receiver = usr.id)
left join thwy_order_worker ow on ( o.id = ow.order_id ) 
left join thwy_worker  fw on ( ow.worker_id = fw.id )
where o.id=:orderId  and ow.worker_id = :userId `,
    {orderId: orderId, userId: userId});

  if (!results) {
    return;
  }

  let items = await dbr.find(`select id, name,quantity,price_payable fee,order_id orderId
from thwy_order_item
where order_id  = :orderId `, {orderId: orderId});

  results.items = items;

  ctx.body = results;
}));
